package com.blog.blog.config;

import com.blog.blog.service.ViewCountService;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class SchedulingConfig {

	private final ViewCountService viewCountService;

	public SchedulingConfig(ViewCountService viewCountService) {
		this.viewCountService = viewCountService;
	}

	// 每 30 秒将 Redis 中的浏览增量合并写回数据库
	@Scheduled(fixedDelay = 30000L, initialDelay = 30000L)
	public void flushViewCounts() {
		viewCountService.flushToDatabase();
	}
}



